package com.smart.campus.school.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.smart.campus.common.annotation.DataScope;
import com.smart.campus.common.annotation.SchoolDataScope;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.common.CommonConstants;
import com.smart.campus.school.domain.*;
import com.smart.campus.school.enums.MsgTypeEnum;
import com.smart.campus.school.mapper.ApproveListMapper;
import com.smart.campus.school.mapper.ClassInfoMapper;
import com.smart.campus.school.mapper.GradeMapper;
import com.smart.campus.school.service.IMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ApplyListMapper;
import com.smart.campus.school.service.IApplyListService;
import com.smart.campus.common.core.text.Convert;

/**
 * 学生请假申请Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-06-15
 */
@Service
public class ApplyListServiceImpl implements IApplyListService 
{
    @Autowired
    private ApplyListMapper applyListMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private ApproveListMapper approveListMapper;
    @Autowired
    private IMsgService msgService;

    /**
     * 查询学生请假申请
     * 
     * @param id 学生请假申请ID
     * @return 学生请假申请
     */
    @Override
    public ApplyList selectApplyListById(Long id)
    {

        ApplyList applyList = applyListMapper.selectApplyListById(id);
        //获取审批人
        ClassInfo classInfo = classInfoMapper.selectClassInfoById(applyList.getClassId());
        String approverIds = classInfo.getTeacherId()+"";
        String approverNames = classInfo.getTeacherName()+"";


        //三天以上需要年级主任审批
        if(DateUtils.getDaySum(applyList.getBeginTime(),applyList.getEndTime()) >= 3){
            Grade grade = gradeMapper.selectGradeById(applyList.getGradeId());
            approverIds+=","+grade.getTeacherId();
            //approverNames+=","+grade.getTeacherName();
        }
        applyList.setApproverIds(approverIds);
        applyList.setApproverNames(approverNames);
        ApproveList approveList = new ApproveList();
        approveList.setApplyId(id);
        List<ApproveList> approveListList = approveListMapper.selectApproveListList(approveList);
        applyList.setApproveListList(approveListList);
        return applyList;
    }

    /**
     * 查询学生请假申请列表
     * 
     * @param applyList 学生请假申请
     * @return 学生请假申请
     */
    @Override
   // @SchoolDataScope(deptAlias = "s")
    public List<ApplyList> selectApplyListList(ApplyList applyList)
    {

        return applyListMapper.selectApplyListList(applyList);
    }

    /**
     * 新增学生请假申请
     * 
     * @param applyList 学生请假申请
     * @return 结果
     */
    @Override
    public int insertApplyList(ApplyList applyList)
    {
        applyList.setCreateTime(DateUtils.getNowDate());
        applyList.setStatus(1);
        //获取审批人
        ClassInfo classInfo = classInfoMapper.selectClassInfoById(applyList.getClassId());
        String approverIds = classInfo.getTeacherId()+"";
        applyList.setSchoolId(classInfo.getDeptId());
        //三天以上需要年级主任审批
        if(DateUtils.getDaySum(applyList.getBeginTime(),applyList.getEndTime()) >= 3){
            Grade grade = gradeMapper.selectGradeById(applyList.getGradeId());
            approverIds+=","+grade.getTeacherId();
        }
        applyList.setApproverIds(approverIds);

        Msg msg = new Msg();
        msg.setUserType(CommonConstants.TEACHER);
        msg.setStatus(CommonConstants.NO);
        msg.setCreateTime(new Date());
        msg.setUserId(classInfo.getTeacherId());
        msg.setUserName(classInfo.getTeacherName());
        msg.setContent(MsgTypeEnum.S_APPLY.getName());
        msg.setMsgType(MsgTypeEnum.S_APPLY.getValue());
        msg.setMsgLink(MsgTypeEnum.S_APPLY.getLink());
        msgService.insertMsg(msg);
        return applyListMapper.insertApplyList(applyList);
    }

    /**
     * 修改学生请假申请
     * 
     * @param applyList 学生请假申请
     * @return 结果
     */
    @Override
    public int updateApplyList(ApplyList applyList)
    {
        applyList.setUpdateTime(DateUtils.getNowDate());
        return applyListMapper.updateApplyList(applyList);
    }

    /**
     * 删除学生请假申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApplyListByIds(String ids)
    {
        return applyListMapper.deleteApplyListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生请假申请信息
     * 
     * @param id 学生请假申请ID
     * @return 结果
     */
    @Override
    public int deleteApplyListById(Long id)
    {
        return applyListMapper.deleteApplyListById(id);
    }
}
